uint8_t getfont68(char c, int i);

class CSmallFont
{
public:
    void Print(int bx, int by, const char* msg, uint16_t color)
    {
        while (*msg)
        {
            Printch(bx, by, *msg++, color);
            bx += 6;
        }
    }
    
    int FitText(char* text, int maxChars, char* line0, char* line1, char* line2)
    {
        auto TakeLine = [](char* text, int maxLength, char* output) -> int
        {
            int lastSpacer = 0;
            
            int len = (int)strlen(text);
            
            if (len < maxLength)
            {
                // fits whole text
                memcpy(output, text, len);
                output[len] = 0;
                return len;
            } else
            {
                len = maxLength;
            }
            
            for (int i=0; i<len; i++)
                if (text[i] == ' ')
                    lastSpacer = i;
            
            if (lastSpacer == 0)
            {
                // cut long word
                memcpy(output, text, maxLength);
                output[maxLength] = 0;
                return maxLength;
            }
            
            memcpy(output, text, lastSpacer);
            output[lastSpacer] = 0;
            return lastSpacer+1;
        };
        
        auto AppendDots = [](char* text, int length)
        {
            if ((int)strlen(text) < length-3)
            {
                strcat(text, "...");
                return;
            }
            strcpy(text+length-3, "...");
        };
        
        line0[0] = line1[0] = line2[0] = 0;
        
        text += TakeLine(text, maxChars, line0);
        if (strlen(text) == 0)
            return 1;
        
        text += TakeLine(text, maxChars, line1);
        if (strlen(text) == 0)
            return 2;
        
        text += TakeLine(text, maxChars, line2);
        if (strlen(text) > 0)
            AppendDots(line2, maxChars);
        return 3;
    }

private:
    void Printch(int bx, int by, char ch, uint16_t c)
    {
        if (ch == ' ')
            return;
        for (int y=0; y<8; y++)
            for (int x=0; x<6; x++)
                if (getfont68(ch, x) & (128>>y))
                    LCD::PutPixel(bx+x, by+y, c);        
    }
};



const static uint8_t CHAR6x8[] = {
    0x00, 0x00, 0xFA, 0xFA, 0x00, 0x00,    //!
    0x00, 0x00, 0xE0, 0x00, 0xE0, 0x00,    //"
    0x00, 0x28, 0xFE, 0x28, 0xFE, 0x28,    //#
    //  0x00, 0x24, 0x74, 0xD6, 0xD6, 0x4C,    //$
    0x00, 0x0C, 0x06, 0x0E, 0x18, 0x30,    //$
    0x00, 0x66, 0x6C, 0x18, 0x36, 0x66,    //%
    0x00, 0x4C, 0xFE, 0x92, 0x9E, 0x6A,    //&
    0x00, 0x00, 0x20, 0xE0, 0xC0, 0x00,    // '
    0x00, 0x00, 0x38, 0x7C, 0xC6, 0x82,    //(
    0x00, 0x00, 0x82, 0xC6, 0x7C, 0x38,    //)
    0x00, 0x54, 0x7C, 0x38, 0x7C, 0x54,    //*
    0x00, 0x10, 0x10, 0x7C, 0x10, 0x10,    //+
    0x00, 0x00, 0x01, 0x07, 0x06, 0x00,    //,
    0x00, 0x10, 0x10, 0x10, 0x10, 0x10,    //-
    0x00, 0x00, 0x00, 0x06, 0x06, 0x00,    //.
    0x00, 0x06, 0x0C, 0x18, 0x30, 0x60,    // /
    0x00, 0x7C, 0xFE, 0x82, 0xFE, 0x7C,    //0
    0x00, 0x00, 0x42, 0xFE, 0xFE, 0x02,    //1
    0x00, 0x46, 0xCE, 0x9A, 0xF2, 0x66,    //2
    0x00, 0x44, 0xC6, 0x92, 0xFE, 0x6C,    //3
    0x00, 0x18, 0x28, 0x48, 0xFE, 0xFE,    //4
    0x00, 0xE2, 0xA2, 0xA2, 0xBE, 0x9C,    //5
    0x00, 0x7C, 0xFE, 0xA2, 0xBE, 0x1C,    //6
    0x00, 0x80, 0x8E, 0xBE, 0xF0, 0xC0,    //7
    0x00, 0x6C, 0xFE, 0x92, 0xFE, 0x6C,    //8
    0x00, 0x70, 0xFA, 0x8A, 0xFE, 0x7C,    //9
    0x00, 0x00, 0x00, 0x24, 0x24, 0x00,    //:
    0x00, 0x00, 0x01, 0x27, 0x26, 0x00,    //;
    //  0x00, 0x10, 0x38, 0x6C, 0xC6, 0x82,    //<
    0x00, 0x18, 0x3C, 0x7E, 0xFF, 0x00,    //
    0x00, 0x28, 0x28, 0x28, 0x28, 0x28,    //=
    //  0x00, 0x82, 0xC6, 0x6C, 0x38, 0x10,    //>
    0x00, 0xFF, 0x7E, 0x3C, 0x18, 0x00,    //
    0x00, 0x40, 0xCA, 0x9A, 0xF0, 0x60,    //?
    0x00, 0x7C, 0xFE, 0x82, 0xB2, 0x74,    //@
    0x00, 0x3E, 0x7E, 0xC8, 0x7E, 0x3E,    //A
    0x00, 0xFE, 0xFE, 0x92, 0xFE, 0x6C,    //B
    0x00, 0x7C, 0xFE, 0x82, 0xC6, 0x44,    //C
    0x00, 0xFE, 0xFE, 0x82, 0xFE, 0x7C,    //D
    0x00, 0xFE, 0xFE, 0x92, 0x92, 0x92,    //E
    0x00, 0xFE, 0xFE, 0x90, 0x90, 0x90,    //F
    0x00, 0x7C, 0xFE, 0x82, 0x92, 0x5C,    //G
    0x00, 0xFE, 0xFE, 0x10, 0xFE, 0xFE,    //H
    0x00, 0x82, 0xFE, 0xFE, 0x82, 0x00,    //I
    0x00, 0x04, 0x82, 0xFE, 0xFC, 0x80,    //J
    0x00, 0xFE, 0xFE, 0x38, 0xEE, 0xC6,    //K
    0x00, 0xFE, 0xFE, 0x02, 0x02, 0x02,    //L
    0x00, 0xFE, 0x70, 0x3C, 0x70, 0xFE,    //M
    0x00, 0xFE, 0xFE, 0x70, 0x38, 0xFE,    //N
    0x00, 0x7C, 0xFE, 0x82, 0xFE, 0x7C,    //O
    0x00, 0xFE, 0xFE, 0x88, 0x88, 0x70,    //P
    0x00, 0x78, 0xFC, 0x86, 0xFE, 0x7A,    //Q
    0x00, 0xFE, 0xFE, 0x98, 0xFC, 0x66,    //R
    0x00, 0x64, 0xF6, 0x92, 0xDE, 0x4C,    //S
    0x80, 0x80, 0xFE, 0xFE, 0x80, 0x80,    //T
    0x00, 0xFC, 0xFE, 0x02, 0xFE, 0xFC,    //U
    0x00, 0xF8, 0xFC, 0x06, 0xFC, 0xF8,    //V
    0x00, 0xFE, 0x1C, 0x78, 0x1C, 0xFE,    //W
    0x00, 0xC6, 0xEE, 0x38, 0xEE, 0xC6,    //X
    0x00, 0xE0, 0xF2, 0x1E, 0xF2, 0xE0,    //Y
    0x00, 0xC6, 0x8E, 0x9A, 0xF2, 0xE6,    //Z
    //  0x00, 0x00, 0xFE, 0x82, 0x82, 0x00,    //[
    0xFF, 0x83, 0x01, 0x7D, 0x01, 0x83,    //0
    0x00, 0x60, 0x30, 0x18, 0x0C, 0x06,    //  "\"
    //  0x00, 0x00, 0x82, 0x82, 0xFE, 0x00,    //]
    0xFF, 0xFF, 0xBD, 0x01, 0x01, 0xFD,    //1
    //  0x00, 0x30, 0x60, 0xC0, 0x60, 0x30,    //^
    0x00, 0x00, 0x2A, 0x2C, 0x28, 0x38,    //^
    0x00, 0x02, 0x02, 0x02, 0x02, 0x02,    //_
    //  0x00, 0x40, 0x70, 0x30, 0x00, 0x00,    //`
    0x28, 0x68, 0xA8, 0x28, 0x00, 0x00,    //`
    0x00, 0x04, 0x2E, 0x2A, 0x2A, 0x1E,    //a
    0x00, 0x7E, 0x7E, 0x12, 0x1E, 0x0C,    //b
    0x00, 0x1C, 0x3E, 0x22, 0x22, 0x22,    //c
    0x00, 0x0C, 0x1E, 0x12, 0x7E, 0x7E,    //d
    0x00, 0x1C, 0x3E, 0x2A, 0x3A, 0x18,    //e
    0x00, 0x10, 0x3E, 0x7E, 0x50, 0x50,    //f
    0x00, 0x1A, 0x3D, 0x25, 0x3F, 0x1E,    //g
    0x00, 0x7E, 0x7E, 0x10, 0x1E, 0x0E,    //h
    0x00, 0x00, 0x12, 0x5E, 0x5E, 0x02,    //i
    0x00, 0x00, 0x01, 0x01, 0x5F, 0x5E,    //j
    0x00, 0x7E, 0x7E, 0x08, 0x1C, 0x36,    //k
    0x00, 0xFC, 0xFE, 0x02, 0x02, 0x00,    //l
    0x00, 0x3E, 0x30, 0x1E, 0x30, 0x3E,    //m
    0x00, 0x3E, 0x3E, 0x20, 0x3E, 0x1E,    //n
    0x00, 0x1C, 0x3E, 0x22, 0x3E, 0x1C,    //o
    0x00, 0x3F, 0x3F, 0x24, 0x3C, 0x18,    //p
    0x00, 0x18, 0x3C, 0x24, 0x3F, 0x3F,    //q
    0x00, 0x3E, 0x3E, 0x10, 0x20, 0x20,    //r
    0x00, 0x14, 0x3A, 0x2A, 0x2E, 0x04,    //s
    0x00, 0x20, 0x7C, 0x7E, 0x22, 0x02,    //t
    0x00, 0x3C, 0x3E, 0x02, 0x3E, 0x3E,    //u
    0x00, 0x38, 0x3C, 0x06, 0x3C, 0x38,    //v
    0x00, 0x3E, 0x0C, 0x18, 0x0C, 0x3E,    //w
    0x00, 0x22, 0x36, 0x1C, 0x36, 0x22,    //x
    0x00, 0x39, 0x3D, 0x05, 0x3F, 0x3E,    //y
    0x00, 0x32, 0x26, 0x2E, 0x3A, 0x36,    //z
    0x09, 0x19, 0xFF, 0x98, 0x88, 0x00,    //
    0x00, 0x00, 0x00, 0xFE, 0x00, 0x00,    //|
    //  0x00, 0x00, 0x2A, 0x2C, 0x28, 0x38,    //{
    //  0x28, 0x68, 0xA8, 0x28, 0x00, 0x00,    //|
    0x90, 0x98, 0xFF, 0x19, 0x11, 0x00,    //
    0x00, 0x30, 0x60, 0x30, 0x18, 0x30,    //~
};

uint8_t getfont68(char c, int i) { return CHAR6x8[(c-'!')*6+i];}
